package com.ytjj.qmyx.data.controller;

import com.ytjj.common.utils.feishu.FeiShuUtil;
import com.ytjj.qmyx.data.amqp.producer.AmqpTestSend;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/amqp")
public class TestController {

    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private AmqpTestSend amqpTestSend;

    @GetMapping("/sendFeishu")
    public String sendFeishu() {
        FeiShuUtil.sendMsg("test data");
        return "ok";
    }

    @GetMapping("/check")
    public String check() {
        return "ok";
    }

    @GetMapping("test")
    public void test() throws Exception {
        amqpTestSend.sendTest("111111");

//        Long increment = redisTemplate.opsForHash().increment("hashInc", "map3", 1);
//
//        System.out.println("-----------" + increment);
//
//        Set<String> hashList = redisTemplate.opsForHash().keys("hashInc");
//        for (String s : hashList) {
//
//        }
//        System.out.println(hashList.toString());
//
//        Map<Object,Object> map = redisTemplate.opsForHash().entries("hashInc");
//        System.out.println("通过entries(H key)方法获取变量中的键值对:" + map);
//        for (Object o : map.keySet()) {
//            System.out.println(o);
//            System.out.println(map.get(o));
//        }
////        System.out.println("--------1111111111111111111111111-------------");
////        Integer h5 = getUv("2021-02-02", "h5", null);
////        System.out.println("-------h5---------" + h5);
////        Integer h51 = getUv1("2021-02-02", "h5", null);
////        System.out.println("-------h51---------" + h51);
//
//        Object o = redisTemplate.opsForHash().get("hashInc", "map3");
//        System.out.println(o);
//        System.out.println("-----------------");
//        Boolean aBoolean = redisTemplate.opsForHash().hasKey("hashInc", "map3");
//        System.out.println(aBoolean);

//        System.out.println(getTransPondNumNew("2021-03-08","h5",null));
        System.out.println("2021-03-08");
    }



    public Integer getTransPondNumNew(String searchDate, String platform, String channel) {
        Criteria criteria = new Criteria();
        if(StringUtils.isNotEmpty(channel)) criteria.and("channel").is(channel);
        return (int) mongoTemplate.count(Query.query(criteria), "skip:" + searchDate);
    }

    public int getOrderUvNumber(String searchDate, String platform, String channel, Integer isAddress, Integer isPay, String activityType, Integer isFirstBuy) {
        Criteria criteria = Criteria.where("platform").is(platform);
        if (StringUtils.isNotEmpty(channel)) criteria.and("channel").is(channel);
        if (StringUtils.isNotEmpty(activityType)) criteria.and("activityType").is(activityType);
        if (null != isAddress) criteria.and("isAddress").is(isAddress);
        if (null != isPay) criteria.and("isPay").is(isPay);
        if (null != isFirstBuy) criteria.and("isFirstBuy").is(isFirstBuy);
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("uuid").count().as("count").last("dayTime").as("dayTime"),
                Aggregation.group("dayTime").count().as("count")
        );
        AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, "order_record:"+ searchDate, Map.class);
        List<Map> maps = results.getMappedResults();
        if (maps.size() == 0) return 0;
        Object count = maps.get(0).get("count");
        return Integer.parseInt(count.toString());
    }

    public int getOrderNumber(String searchDate, String platform, String channel, Integer isAddress, Integer isPay, String activityType) {
        Criteria criteria = Criteria.where("platform").is(platform);
        if (StringUtils.isNotEmpty(channel)) {
            criteria.and("channel").is(channel);
        }
        if(StringUtils.isNotEmpty(activityType)) {
            criteria.and("activityType").is(activityType);
        }
        if(null != isAddress) {
            criteria.and("isAddress").is(isAddress);
        }
        if(null != isPay) {
            criteria.and("isPay").is(isPay);
        }
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("uuid").count().as("count").last("dayTime").as("dayTime"),
                Aggregation.group("dayTime").count().as("count")
        );
        AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, "order_record:" + searchDate, Map.class);
        List<Map> maps = results.getMappedResults();
        if (maps.size() == 0) return 0;
        Object count = maps.get(0).get("count");
        return Integer.parseInt(count.toString());
    }

    /**
     * 获取uv
     *
     * @param searchDate
     * @param channel
     * @return
     */
    public Integer getUv(String searchDate, String platform, String channel) {
        log.info("\n开始获取页面UV：{}\n",System.currentTimeMillis());
        Criteria criteria = this.commonCriteria(platform, channel, searchDate, null);
        List<Map> maps = aggByUuid(criteria);
        if(maps.size() == 0) return 0;
        Object count = maps.get(0).get("count");
        log.info("\n结束获取页面UV：{}\n",System.currentTimeMillis());
        return Integer.parseInt(count.toString());
    }

    public List<Map> aggByUuid(Criteria criteria) {
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("uuid").count().as("count").last("dayTime").as("dayTime")
                ,Aggregation.group("dayTime").count().as("count")
        );
        AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, "page_statistics", Map.class);
        return results.getMappedResults();
    }

    /**
     * 获取uv
     *
     * @param searchDate
     * @param channel
     * @return
     */
    public Integer getUv1(String searchDate, String platform, String channel) {
        log.info("\n开始获取页面UV：{}\n",System.currentTimeMillis());
        Criteria criteria = this.commonCriteria(platform, channel, searchDate, null);
        Object count = aggregateByUuid(criteria).size();
        log.info("\n结束获取页面UV：{}\n",System.currentTimeMillis());
        return Integer.parseInt(count.toString());
    }

    /**
     * 通用查询
     *
     * @param channel
     * @return
     */
    public Criteria commonCriteria(String platform, String channel, String searchDate, String pathCode) {
        Criteria criteria = Criteria.where("dayTime").is(searchDate);
        if (StringUtils.isNotEmpty(channel)) {
            criteria.and("channel").is(channel);
        }
        if (StringUtils.isNotEmpty(platform)) {
            criteria.and("platform").is(platform);
        }
        if(StringUtils.isNotEmpty(pathCode)) {
            criteria.and("pathCode").is(pathCode);
        }
        return criteria;
    }


    public List<Map> aggregateByUuid(Criteria criteria) {
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("uuid").count().as("count").last("uuid").as("uuid")
        );
        AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, "page_statistics", Map.class);
        return results.getMappedResults();
    }


}
